module.exports = {
	load: function (fn) {
		return document.addEventListener("DOMContentLoaded", fn);
	},
	tab: function tab(tabsecor, tabActive, changesecor, changeOn) {
		document.querySelectorAll(tabsecor).forEach(function (v, i) {
			v.addEventListener("click", function () {
				if (document.querySelector(`${tabsecor}.${tabActive}`))
					document
						.querySelector(`${tabsecor}.${tabActive}`)
						.classList.remove(tabActive);
				this.classList.add(tabActive);
				if (changesecor) {
					if (document.querySelector(`${changesecor}.${changeOn}`))
						document
							.querySelector(`${changesecor}.${changeOn}`)
							.classList.remove(changeOn);
					document.querySelectorAll(changesecor)[i].classList.add(changeOn);
				}
			});
		});
	},
	addTabs(type) {
		const body = document.querySelector("body");
		const tabs = document.createElement("div");
		tabs.id = "tab";
		tabs.innerHTML = `<ul class = 'container'>
		<li>
			<a href="./index.html" class = ${type === "index" ? "active" : ""}>
				<span class="iconfont icon-shouye"></span>
				<p>首页</p>
			</a>
		</li>
		<li>
			<a href="./sports_course.html" class = ${type === "sport" ? "active" : ""}>
				<span class="iconfont icon-xindiantu"></span>
				<p>运动</p>
			</a>
		</li>
		<li>
			<a href="./circle_follow.html" class = ${type === "circle" ? "active" : ""}>
				<span class="iconfont icon-quanzi"></span>
				<p>圈子</p>
			</a>
		</li>
		<li>
			<a href="./mine.html" class = ${type === "mine" ? "active" : ""}>
				<span class="iconfont icon-wode1"></span>
				<p>我的</p>
			</a>
		</li>
	</ul>`;
		body.appendChild(tabs);
	},
	formatTime(number) {
		if (number > 0) {
			let h = Math.floor(number / 3600);
			let m = Math.floor((number % 3600) / 60);
			let s = number % 60;
			return [h, m, s].map((v) => (v < 10 ? "0" + v : v)).join(":");
		} else {
			return "_ _";
		}
	},
	//补零
	padZero(number) {
		return number < 10 ? "0" + number : number;
	},
	//获取元素
	/**
	 * @deprecated 实现元素的获取
	 * @param {string} selector选择器
	 * @returns
	 */
	$: function $(selector) {
		let ss = document.querySelectorAll(selector);
		switch (ss.length) {
			case 1:
				return ss[0];
			case 0:
				return console.warn(`${selector}没有获取到节点`);
			default:
				return ss;
		}
	},

	//返回n-m的随机数
	/**
	 * @deprecated 获取两数之间的随机数
	 * @param {Number} m 小的数
	 * @param {Number} n 大的数
	 * @returns
	 */
	getRandom(m = 0, n = 0) {
		return Math.floor(Math.random() * (n - m + 1) + m);
	},

	//本地存取
	local: {
		//得到
		/**
		 * @deprecated 从本地获取序列化的字符串并转化
		 * @param {*} key 键名
		 * @returns
		 */
		getItem(key) {
			return JSON.parse(localStorage.getItem(key));
		},
		/**
		 * @deprecated 把字符串序列化并存储到本地
		 * @param {*} key 键名
		 * @param {*} value 键值
		 * @returns
		 */
		//添加
		setItem(key, value) {
			localStorage.setItem(key, JSON.stringify(value));
		},
		/**
		 * @deprecated 删除本地名为key的键值对
		 * @param {*} key 键名
		 * @returns
		 */
		//删除
		removeItem(key) {
			localStorage.removeItem(key);
		},
		/**
		 * @deprecated 删除本地所有
		 * @returns
		 */
		//全部删除
		clear() {
			localStorage.clear();
		},
	},
};
